use std::collections::HashMap;
use std::time::Duration;
use std::sync::Arc;

use crate::actor::Props;

/// Remote configuration
#[derive(Clone)]
pub struct RemoteConfig {
    /// Host to bind to
    pub host: String,
    /// Port to bind to
    pub port: u16,
    /// Timeout for remote operations
    pub timeout: Duration,
    /// Actor kinds
    pub kinds: Arc<HashMap<String, Props>>,
}

impl RemoteConfig {
    /// Create a new remote configuration
    pub fn new(host: impl Into<String>, port: u16) -> Self {
        Self {
            host: host.into(),
            port,
            timeout: Duration::from_secs(5),
            kinds: Arc::new(HashMap::new()),
        }
    }
    
    /// Register an actor kind
    pub fn register_kind(self, kind: impl Into<String>, props: Props) -> Self {
        let mut kinds_map = (*self.kinds).clone();
        kinds_map.insert(kind.into(), props);
        Self {
            host: self.host,
            port: self.port,
            timeout: self.timeout,
            kinds: Arc::new(kinds_map),
        }
    }
    
    /// Set the connection timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }
}

impl Default for RemoteConfig {
    fn default() -> Self {
        Self::new("127.0.0.1", 8090)
    }
}

/// Configuration builder for remote settings
pub struct Config {
    config: RemoteConfig,
}

impl Config {
    /// Create a new config with default values
    pub fn new() -> Self {
        Self {
            config: RemoteConfig::default(),
        }
    }
    
    /// Set the host address
    pub fn with_host(mut self, host: &str) -> Self {
        self.config.host = host.to_string();
        self
    }
    
    /// Set the port
    pub fn with_port(mut self, port: u16) -> Self {
        self.config.port = port;
        self
    }
    
    /// Set the connection timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.config.timeout = timeout;
        self
    }
    
    /// Register an actor kind
    pub fn with_kind(mut self, kind: &str, props: Props) -> Self {
        self.config = self.config.register_kind(kind, props);
        self
    }
    
    /// Get the final configuration
    pub fn build(self) -> RemoteConfig {
        self.config
    }
} 